<?php

namespace backend\modules\business\controllers;

use backend\controllers\BaseController;
use yii\helpers\Url;
use yii\data\Pagination;
use common\models\BusConfig;
use common\extended\system\func;
/**
 * 系统设置
 * @author      futao
 * @date        2016-2-22
 * @version     2.0
 * @copyright   Copyright 2016 meelier.com
 * type 1  充值卡
 * type 2  疗程卡
 * type 3  合作项目
 * type 4  服务项目
 * type 5  房间
 * type 6  库存
 */
class ConfigController extends BaseController
{
	public function actionIndex(){
		
		$this->redirect(Url::toRoute('config/list'));
	}
	/**
	 * 卡项列表
	 * @date: 2016-2-22 
	 * @author: futao
	 */
    public function actionList()
    {	
    	$model = new BusConfig();
    	if(!$model->initData()){
    		$this->error("初始化数据失败!");
    	}
		$type = $this->request->get('type',0);
		$cat = $this->request->get('cat',1);
    	if(!in_array($type,[1,2,3,4,5,6])){
    		$this->error("参数错误！");
    	}
    	$where = $this->where;
	    $where[] = 'config_type = :type';
	    $bind[':type'] = $type;
	    if($type==6){
	    	$where[] = 'config_key = :key';
	    	$bind[':key'] = func::getTitle($cat);
	    }
	    $whereStr = implode(" and ",$where);
	    $query = BusConfig::find()
	    ->where($whereStr,$bind);
	    $countquery = clone $query;
	    $pages=new Pagination(['totalCount' => $countquery->count(),'pageSize' => $this->pageNavLimit]);
	    $list = $query
	    ->offset($pages->offset)
	    ->limit($pages->limit)
	    ->all();
    	return $this->render('index'.$type,['list'=>$list,'page'=>$pages,'type'=>$type,'cat'=>$cat]);
    }
    /**
     * 添加卡项
     * @date: 2016-2-23 
     * @author: futao
     */
    public function actionAdd(){
    	$type = $this->request->post('type',0);
    	$key = $this->request->post('key','');
    	$val = $this->request->post('val','');
    	$val2 = $this->request->post('val2','');
    	if($key=='' || !in_array($type,[1,2,3,4,5,6]) || $val==''){
    		$this->error('参数错误！');
    	}
    	if($type == 1){
    		$count = BusConfig::find()->where("shop_id = :shop_id and config_type = 1 and config_key = :key",[':shop_id'=>$this->userInfo->shop_id,':key'=>$key])->count();
    		if($count>0)$this->error('会员卡名不能重复！');
    	}
    	$configModel = new BusConfig();
    	$configModel->config_type = $type;
    	$configModel->config_key = $key;
    	$configModel->config_val = $val;
    	$configModel->config_val2 = $val2;
    	$configModel->shop_id =  $this->userInfo->shop_id;
    	if(!$configModel->save(false)){
    		$this->error('新增失败！');
    	}
    	$this->redirect(Url::toRoute(['config/list','type'=>$type,'cat'=>intval($this->request->post('cat',1))]));
    }
    /**
     * 用于规格删除
     * @date: 2016-2-24 
     * @author: futao
     */
    public function actionDel(){
    	$id = intval($this->request->get('id',0));
    	if($id < 1){
    		$this->error('请求数据错误！');
    	}
    	$where = $this->where;
    	$where[] = 'config_id = :id';
    	$bind[':id'] = $id;
    	$where[] = 'config_type = 6';
    	$whereStr = implode(" and ", $where);
    	$info = BusConfig::find()
    	->where($whereStr,$bind)
    	->one();
    	if(!$info){
    		$this->error('请求数据错误！');
    	}
    	if(!$info->delete()){
    		$this->error('删除失败！');
    	}
    	$this->redirect(\Yii::$app->request->referrer);
    }
    /**
     * 编辑
     * @date: 2016-2-24 
     * @author: futao
     */
    public function actionUpdate(){
    	$id = intval($this->request->post('id',0));
    	$key = $this->request->post('key','');
    	$val = $this->request->post('val','');
    	$val2 = $this->request->post('val2','');
		$type = $this->request->post('type',1);
    	if($key=='' || $val=='' || !in_array($type,[1,2,3,4,5,6]) || $id<1){
    		$this->error('参数错误！');
    	}
    	if($type == 1){
    		$count = BusConfig::find()->where("shop_id = :shop_id and config_type = 1 and config_key = :key and config_id != :id",[':shop_id'=>$this->userInfo->shop_id,':key'=>$key,'id'=>$id])->count();
    		if($count>0)$this->error('会员卡名不能重复！');
    	}
    	$where = $this->where;
    	$where[] = "config_id = $id";
    	$whereStr = implode(" and ",$where);
    	$info = BusConfig::find()->where($whereStr)->one();
    	if(!$info){
    		$this->error("数据不存在！");
    	}
    	$info->config_key = $key;
    	$info->config_val = $val;
    	$info->config_val2 = $val2;
    	if(!$info->save(false)){
    		$this->error('保存失败！');
    	}
    	$this->redirect(Url::toRoute(['config/list','type'=>$type,'cat'=>intval($this->request->post('cat',1))]));
    }   
}